home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: in1.uu.net!bcstec!schultz
- From: schultz@bcstec.ca.boeing.com (Robert A. Schultz)
- Subject: Re: Newbie question: Is this code OK?
- Message-ID: <DLC72M.H4v@bcstec.ca.boeing.com>
- Organization: The Boeing Company
- X-Newsreader: TIN [version 1.2 PL2]
- References: <4di986$fk1@pegasus.interpac.net>
- Date: Wed, 17 Jan 1996 17:54:22 GMT
-
- Stacy Sherman (stacys@isis.interpac.net) wrote:
- : I'm learning C and would like to know your opinions on my answer to an
- : exercise. I was supposed to write a function that counts the number of
- : words in a string (array of char in this case, with the length specified
- : in a variable). Of course the biggest problem is stripping out all the
- : extraneous white space that may be there. In this exercise, whitespace
- : separating words was either a space, tab or newline character.
-
- Thanks for writing the code and then asking for opinions.
-
- : The code I wrote worked on every case I gave it but I want to know if it
- : was well written. Specifically:
-
- : I know it's usually not a good idea to increment a loop counter within a
- : loop, but I have 2 nested loops using the same counter. Was this a bad
- : thing to do?
-
- Not really a bad idea provided you are careful that the end conditions of
- both loops do not conflict with each other. In your case you do check
- i<length is both so I would have no problem with this code. Personal
- preference would have led me to use a while loop in the second case but
- that is just cosmetic.
-
- : I have an if statement and a for loop that test for the same things. Is
- : it possible to somehow combine these into one statement? I couldn't
- : think of a way to do it.
-
- To be honest nothing leaps to my mind right of the top of my head either.
-
- : Do I need the empty {} after a for loop that has no body? I assumed if I
- : didn't, the loop would execute the next statement after it.
-
- No. The empty {} is not required. Simply replace it with a ';' and all will
- be fine. Again I would consider this to be a cosmetic change.
-
- : Any other comments? Here's the code:
-
- 1) So what happens if length is > 80? Unless you are *very* sure of you
- input data you should always check for your string being terminated
- in some unexpected place. You could simply watch for string[i] = '\0'
- in your case. The moral of the story here is that careful checking of
- input data will save you a lot of pain down the road.
- 2) I am not sure of the requirements of the assignment but I would have
- written the "char string[80]" in the declaration as a more general
- "char *string" or "char string[]". Of course I would then also add
- a check for "string == NULL"
- 3) You can check for string[i] being '\n', '\t' or ' ' by calling isspace().
- E.g. "if (!isspace(string[i])) ...
- 4) It looks to me like you have a grasp of what is going on.
-
-
- : int words (char string[80], int length)
- : {
- : int i, num_words = 0;
- :
- : for (i=0; i<length; i++)
- : { /* if char isn't whitespace */
- : if ((string[i]!='\n') &&
- : (string[i]!='\t') &&
- : (string[i]!=' '))
- : {
- : num_words++; /* Increment num_words */
- : for (; i<length, ((string[i]!='\n') &&
- : (string[i]!='\t') &&
- : (string[i]!=' ')); i++)
- : /* and skip past any other */
- : /* chars that may be there */
- : { } /* loop does all work, nothing inside body */
- : }
- : }
- : return (num_words);
- : }
- --
- =''' Rob Schultz
- c-oo TSAP Programmer Guy
- \ Boeing Tech Services Systems
- - veni vidi recodei (206) 544-0793 MS 2H-31
-